
/**
 * ********************
 * 1.主页(index)
 * ********************
 * 2.登录页面(login-screen)
 * ********************
 * 3.通讯录页面(contact)
 * ********************
 * 4.开会签到页面(meeting-check)
 * ********************
 * 5.早起(morning-check)
 * ********************
 * 6.晚归(evening-check)
 * ********************
 * 7.部门简介(introduction-department)
 * ********************
 * 8.通报表扬申请页面(apply-for-by)
 * ********************
 * 9.通报批评申请页面(apply-for-pp)
 * ********************
 * 10.面试(interview)
 * 该页面将，提交了申请的所有人，列出来，面试通过的标记通过，没通过的也要标记没通过
 * ********************
 * 11.统计报表页面(notification-detail-chart)
 * ********************
 * 12.早操页面(exercise-check)
 * ********************
 * 13.通报表扬详情页面(notification-detail-by)
 * ********************
 * 14.通报批评详情页面
 * ********************
 * */

// 标记是否登录
var login_stutus = false;
// Initialize your app
var myApp = new Framework7({
	cache:false,
	smartSelectBackOnSelect : true,
	precompileTemplates: true,
	template7Pages: true,
	template7Data: {}
});

// Export selectors engine
var $$ = Dom7;


// Add view
var mainView = myApp.addView('.view-main', {
    dynamicNavbar: true
});

/*===== 1.主页 =====*/
cookieLogin();
myApp.onPageAfterAnimation('index',function(page){
	checkLoginStutus(myApp.mydata);
})
/*===== 2.登录页面 =====*/

myApp.onPageInit('login-screen', function (page) {
  
  var pageContainer = $$(page.container);
  
  pageContainer.find('.login-btn').on('click', function () {
	    var username = pageContainer.find('input[name="username"]').val();
	    var password = pageContainer.find('input[name="password"]').val();
	    myApp.showPreloader('登录中');
	    $.ajax({
	    	url:'jsondata/data.json',
	    	dataType:'json' ,
	    	data:{
	    		stu_no:username,
	    		stu_pass:password
	    	},
	    	success:function(data){
	    		if(username == data.testData.stu_no && password == data.testData.stu_pass && data.login_stutus){
	    			
	    			myApp.hidePreloader();
	    			login_stutus = true;
	    			myApp.template7Data["page:index"] = data;
	    			myApp.alert('登录成功','',function(){
	    				mainView.back();
	    			});
	    			
	    			$.cookie('username',username,{'expires':7});
	    			$.cookie('password',password,{'expires':7});
	    		}
	    		else{
	    			
	    			myApp.hidePreloader();
	    			myApp.alert('用户名或密码错误');
	    		}
	    	},
	    	error:function(){
	    		myApp.hidePreloader();
	    		myApp.alert('访问出错了，请重新登录');
	    	}
	    });
		
  });
  
});


/*===== 3.通讯录页面 =====*/
myApp.onPageAfterAnimation('contact', function (page) {
	
	mainView.router.refreshPage();
	
});

myApp.onPageBeforeInit('contact',function(page){
	myApp.showPreloader('加载中');
	$.ajax({
		url:"jsondata/contactData.json",
		dataType:'json',
		data:{},
		success:function(contact_data){

			myApp.template7Data["url:contact.html"] = contact_data;
			
			myApp.hidePreloader();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
            myApp.alert('加载失败，请刷新页面');
        }
	});
});

/*===== 4.开会签到 =====*/
myApp.onPageAfterAnimation('meeting-check', function (page) {
	
	mainView.router.refreshPage();
	
});
myApp.onPageBeforeInit('meeting-check', function (page) {
	myApp.showPreloader('加载中');
	$.ajax({
		url:"jsondata/contactData.json",
		dataType:'json',
		data:{},
		success:function(contact_data){

			myApp.template7Data["url:meeting-check.html"] = contact_data;
			
			myApp.hidePreloader();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
            myApp.alert('加载失败，请刷新页面');
        }
	});
});
/*===== 5.查早起 =====*/
myApp.onPageAfterAnimation('morning-check',function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('morning-check',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/dormitory.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:morning-check.html"] = data;
		}
	});
})

myApp.onPageInit('morning-check',function(page){
	if(page.name == 'morning-check'){
		
		$('.dormitory-check').click(function(e){
			
			e.stopPropagation();
			
		})
		
		$$('.dormitory-check').on('click', function () {
			var _this = $$(this);
		    var buttons = [
		        {
		            text: '拒检',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		        {
		            text: '未挂大锁',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		        {
		            text: '贴可示窗',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		        {
		        	text:'差寝',
		        	onClick: function (){
		        		_this.val( this.text );
		        	}
		        },
		        {
		        	text:'优寝',
		        	onClick: function (){
		        		_this.val( this.text );
		        	}
		        },
		        {
		            text: '寝室情况(无)',
		            color: 'red',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		    ];
		    myApp.actions(buttons);
		});  
	}
});

/*===== 6.查晚归 =====*/
myApp.onPageAfterAnimation('evening-check',function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('evening-check',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/dormitory.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:evening-check.html"] = data;
		}
	});
})
myApp.onPageInit('evening-check',function(page){
	if(page.name == 'evening-check'){
		$('.dormitory-check').click(function(e){
			
			e.stopPropagation();
			
		})
		
		$$('.dormitory-check').on('click', function () {
			var _this = $$(this);
		    var buttons = [
		        {
		            text: '拒检',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		        {
		            text: '大功率',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		        {
		        	text:'挂锁',
		        	onClick: function (){
		        		_this.val( this.text );
		        	}
		        },
		        {
		            text: '寝室情况(无)',
		            color: 'red',
		            onClick: function () {
		                _this.val( this.text );
		            }
		        },
		    ];
		    myApp.actions(buttons);
		});  
	}
});

/*===== 7.部门简介 =====*/
myApp.onPageInit('introduction-department',function(page){
	var mySwiper = myApp.swiper('.swiper-container', {
	  pagination: '.swiper-pagination',
	  paginationHide: false,
	  paginationClickable: true,
	  nextButton: '.swiper-button-next',
	  prevButton: '.swiper-button-prev',
	  loop:true,
	  spaceBetween:100,
	  speed:400
	});  
});

/*===== 8.通报表扬申请页面 =====*/
myApp.onPageAfterAnimation('apply-for-by',function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('apply-for-by',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/all_user.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:apply-for-by.html"] = data;
		}
	});
});
/*===== 9.通报批评申请页面 =====*/
myApp.onPageAfterAnimation('apply-for-pp',function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('apply-for-pp',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/all_user.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:apply-for-pp.html"] = data;
		}
	});
});
/*===== 10.面试 =====*/
myApp.onPageAfterAnimation('interview',function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('interview',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/interview_data.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:interview.html"] = data;
		}
	});
});

/*===== 11.报表页面 =====*/

myApp.onPageBeforeInit('notification-detail-chart',function(page){
	myApp.showPreloader('载入中');
	Highcharts.setOptions({
	    "lang": {
	    	"printChart":"打印图表",
	    	"downloadJPEG":"下载JPEG图片",
	    	"downloadPDF": "下载PDF文档",
	    	"downloadPNG": "下载PNG 图片"  ,
	        "downloadSVG": "下载SVG 矢量图" ,
	      	"exportButtonTitle": "导出图片"
	    }
	});
	$.ajax({
		url:"jsondata/violations-data.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			
			var no_back = [],
			late_back = [],
			no_lesson = [],
			illegal_use_of_electricity = [],
			refuse_to_check = [],
			no_make_bed=[],
			no_wake_up = [],
			no_locked=[],
			stick_windows=[],
			morning_exercises_absence=[],
			meeting_absence = [],
			leave_off=[],
			other = [],
			classes = []
			// 各班违纪总人次
			var all_class_array = [],
			// 年级违纪总人次
			sum = 0,
			// 各班违纪率
			all_class_violation_rate = [];
			
			

			for(var i = 0;i < data.class.length;i++){
				var _this = data.class[i];
				all_class_array.push(0);
				for(var type in _this){
					switch ( type ){
						case "name":
							classes.push( _this[type] );
							break;
						case "no-back":
							no_back.push( _this[type] );
							break;
						case "late-back":
							late_back.push( _this[type] );
							break;
						case "no-lesson":
							no_lesson.push( _this[type] );
							break;
						case "illegal-use-of-electricity":
							illegal_use_of_electricity.push( _this[type] );
							break;
						case "refuse-to-check":
							refuse_to_check.push( _this[type] );
							break;
						case "no-make-bed":
							no_make_bed.push( _this[type] );
							break;
						case "no-wake-up":
							no_wake_up.push(  _this[type] );
							break;
						case "no-locked":
							no_locked.push( _this[type] );
							break;
						case "stick-windows":
							stick_windows.push( _this[type] );
							break;
						case "morning-exercises-absence":
							morning_exercises_absence.push( _this[type] );
							break;
						case "meeting-absence":
							meeting_absence.push( _this[type] );
							break;
						case "leave-off":
							leave_off.push( _this[type] );
							break;
						case "other":
							other.push( _this[type] );
							break;
						default:
							break;
					}
					
					if(type !== "name"){
						all_class_array[i] += _this[type];
					}
					
				}
			}

			for( var i = 0; i < data.class.length ;i++ ){
				sum += all_class_array[i];
			}
			
			for( var i = 0; i < data.class.length ;i++ ){
				var obj = [data.class[i]["name"] , all_class_array[i] / sum]
				all_class_violation_rate.push( obj );
			}
			
			var chart = new Highcharts.Chart({
				"chart":{
					"renderTo":"container"
				},
			    "title": {
			        "text": "各班违纪汇总图表"
			    },
			    "subtitle":{
			    	"text": "2015-10-10"
			    },
			    "xAxis": {
			        "categories": classes,
        			"crosshair":true
			    },
			    "yAxis":{
			    	"title":{
			    		"text":"违 纪 人 数"
			    	}
			    },
			    "series": [{
			        "type": "column",
			        "name": "旷课",
			        "data": no_lesson
			    }, {
			        "type": "column",
			        "name": "未归",
			        "data": no_back
			    }, {
			        "type": "column",
			        "name": "晚归",
			        "data": late_back
			    }, {
			        "type": "column",
			        "name": "违章用电",
			        "data": illegal_use_of_electricity
			    }, {
			        "type": "column",
			        "name": "拒检",
			        "data": refuse_to_check
			    }, {
			        "type": "column",
			        "name": "未叠被子",
			        "data": no_make_bed
			    }, {
			        "type": "column",
			        "name": "未起床",
			        "data": no_wake_up
			    }, {
			        "type": "column",
			        "name": "未挂大锁",
			        "data": no_locked
			    }, {
			        "type": "column",
			        "name": "贴可视窗",
			        "data": stick_windows
			    }, {
			        "type": "column",
			        "name": "缺早操",
			        "data": morning_exercises_absence
			    }, {
			        "type": "column",
			        "name": "大会缺席",
			        "data": meeting_absence
			    }, {
			        "type": "column",
			        "name": "其他违纪",
			        "data": other
			    }, {
			        "type": "column",
			        "name": "请假",
			        "data": leave_off
			    }, {
			        "type": "spline",
			        "name": "总违纪数",
			        "data": all_class_array,
			        "marker": {
			            "lineWidth": 2,
			            "lineColor": "green",
			            "fillColor": "white"
			        }
			    }]
			});
			var chart2 = new Highcharts.Chart({
	            chart: {
	                plotBackgroundColor: null,
	                plotBorderWidth: null,
	                plotShadow: false,
	                renderTo:"container2"
	            },
	            title: {
	                text: '15级各班违纪率'
	            },
	            tooltip: {
	                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	            },
	            plotOptions: {
	                pie: {
	                    allowPointSelect: true,
	                    cursor: 'pointer',
	                    dataLabels: {
	                        enabled: true
	                    },
	                    showInLegend: true
	                }
	            },
	            series: [{
	                type: "pie",
	                name: "各班违纪率",
	                data: all_class_violation_rate
	            }]
	        });
			
		}
	});
})
/*===== 12.早操 =====*/
myApp.onPageAfterAnimation("exercise-check",function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('exercise-check',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/all_user.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:exercise-check.html"] = data;
		}
	});
});
/*===== 13.通报表扬详情页面 =====*/
myApp.onPageAfterAnimation("notification-detail-by",function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('notification-detail-by',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/notification.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:notification-detail-by.html"] = data;
		}
	});
});
/*===== 14.通报批评详情页面 =====*/
myApp.onPageAfterAnimation("notification-detail-pp",function(page){
	mainView.router.refreshPage();
});
myApp.onPageBeforeInit('notification-detail-pp',function(page){
	myApp.showPreloader('载入中');
	$.ajax({
		url:"jsondata/notification-pp.json",
		data:{},
		dataType:'json',
		success:function(data){
			myApp.hidePreloader();
			myApp.template7Data["url:notification-detail-pp.html"] = data;
		}
	});
});
